﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using BPM.Foundation.WFDB;
using BPM.Base.Tools;

namespace BPM.Foundation.WFService
{
    public partial interface IWorkflowService
    {
        [System.ServiceModel.OperationContract]
        FormTemplet getFormTemplet(string formName);

        [System.ServiceModel.OperationContract]
        List<FM_Templet> getFormTempletList();

        [System.ServiceModel.OperationContract]
        int? removeFormTemplet(string formName);

        [System.ServiceModel.OperationContract]
        int? addFormTemple(FormTemplet formTemplet);

        [System.ServiceModel.OperationContract]
        int? createForm(string formName, string businessID, string formID, string editEdition);

        [System.ServiceModel.OperationContract]
        int? removeForm(string formID, string editEdition);

        [System.ServiceModel.OperationContract]
        int? updateForm(Form form);

        [System.ServiceModel.OperationContract]
        List<FM_Form> getForms(string formName, string businessID);

        [System.ServiceModel.OperationContract]
        FormV getFormV(string formID, string editEdition);

        [System.ServiceModel.OperationContract]
        List<FM_Form> getFormEditionList(string formID);

        [System.ServiceModel.OperationContract]
        string  upFile(string filename, byte[] content);

        [System.ServiceModel.OperationContract]
        byte[] downFile(string fileID);

        [System.ServiceModel.OperationContract]
        string getDataFormFiled(Guid guid, string fieldName, string expression);

    }
    public partial class WorkflowService : IWorkflowService
    {
        //12.1	√得到表单模板
        public FormTemplet getFormTemplet(string formName)
        {
            var v = new FormTemplet();

            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (wfDB.FM_Templet.Count(p => p.formName == formName) == 1)
                {
                    v.templet = wfDB.FM_Templet.Single(p => p.formName == formName);
                    v.templetItem = wfDB.FM_TempletItem.Where(p => p.formName == formName).ToList();
               
                }
                else
                {
                    return null ;
                }


            }
           
            return v;
        }

        //12.2	√得到表单模板列表
        public List<FM_Templet> getFormTempletList()
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                return wfDB.FM_Templet.ToList();
            }
        }

        //12.3	√删除表单模板
        public int? removeFormTemplet(string formName)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (wfDB.FM_Templet.Count(p => p.formName == formName) == 1)
                {
                    var v = wfDB.FM_Templet.Single(p => p.formName == formName);
                    wfDB.FM_Templet.DeleteOnSubmit(v);
                    try
                    {
                        wfDB.SubmitChanges();
                    }
                    catch (System.Exception ex)
                    {
                        MyLog.writeError(ex.Message);
                    }
                    return null;
                }
                else
                {
                    return 1;
                }

                
            }
        }

        //12.4	√添加表单模板
        public int? addFormTemple(FormTemplet formTemplet)
        {
            if (formTemplet == null)
            {
                return 1;
            }
            if (formTemplet.templet == null)
            {
                return 2;
            }
            if (formTemplet.templetItem == null)
            {
                return 3;
            }
            if (string.IsNullOrEmpty( formTemplet.templet.formName))
            {
                return 4;
            }

            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (wfDB.FM_Templet.Count(p => p.formName == formTemplet.templet.formName) != 0)
                {
                    return 5;
                }
                else
                {
                    wfDB.FM_Templet.InsertOnSubmit(formTemplet.templet);

                    try
                    {
                        wfDB.SubmitChanges();
                    }
                    catch (System.Exception ex)
                    {
                        MyLog.writeError(ex.Message);
                    }

                    foreach (var ti in formTemplet.templetItem)
                    {
                        ti.formName = formTemplet.templet.formName;
                        ti.itemGuid = System.Guid.NewGuid();
                        wfDB.FM_TempletItem.InsertOnSubmit(ti);

                    }
                    try
                    {
                        wfDB.SubmitChanges();
                    }
                    catch (System.Exception ex)
                    {
                        MyLog.writeError(ex.Message);
                    }
                }
            }

            return null;
        }

        //12.5	√创建表单
        public int? createForm(string formName, string businessID, string formID,string editEdition)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (wfDB.FM_Templet.Count(p => p.formName == formName) == 0)
                {
                    return 1;
                }

                if (wfDB.FM_Form.Count(p => p.formID == formID && p.editEdition ==editEdition) != 0)
                {
                    return 2;
                }

                //-

                var t = wfDB.FM_Templet.Single(p => p.formName == formName);
                var ti = wfDB.FM_TempletItem.Where(p => p.formName == formName);

                var f = new FM_Form();
                f.formID = formID;
                f.businessID = businessID;
                f.editEdition = editEdition;
                f.formName = t.formName;
                f.isComplete = 0;
            
                wfDB.FM_Form.InsertOnSubmit(f);

                foreach (var v in ti)
                {
                    var fi = new FM_Item();
                    fi.autoID = System.Guid.NewGuid();
                    fi.formID = formID;
                    fi.instanceID = System.Guid.Empty;
                    fi.isComplete = 0;
                    fi.itemGuid = v.itemGuid;
                    fi.itemValue = v.itemValue;
                    fi.rowID = "0";
                    fi.stateName = "";
                    fi.editEdition = f.editEdition;
                    fi.tag = v.D ;
                    wfDB.FM_Item.InsertOnSubmit(fi);
                }

                try
                {
                    wfDB.SubmitChanges();
                }
                catch (System.Exception ex)
                {
                    MyLog.writeError(ex.Message);
                }

            }

            return null;
        }

        //12.6	√删除表单
        public int? removeForm(string formID, string editEdition)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (wfDB.FM_Form.Count(p => p.formID == formID && p.editEdition == editEdition) != 1)
                {
                    return 1;
                }
                else
                {
                    var v = wfDB.FM_Form.Single(p => p.formID == formID && p.editEdition == editEdition);

                    wfDB.FM_Form.DeleteOnSubmit(v);
                    try
                    {
                        wfDB.SubmitChanges();
                    }
                    catch (System.Exception ex)
                    {
                        MyLog.writeError(ex.Message);
                    }
                    return null;
                }
            }

       
        }

        //12.7	√更新表单
        public int? updateForm(Form form)
        {
            var v = removeForm(form.formHard.formID, form.formHard.editEdition);
            if (v != null)
            {
                return 1;
            }
            //-
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
 

               wfDB.FM_Form.InsertOnSubmit(form.formHard);

               foreach (var it in form.items)
               {
                   wfDB.FM_Item.InsertOnSubmit(it);
               }
               try
               {
                   wfDB.SubmitChanges();
               }
               catch (System.Exception ex)
               {
                   MyLog.writeError(ex.Message);
               }

            }


            return null;
        }

        //12.8	√得到指定业务表单集合
        public List<FM_Form> getForms(string formName, string businessID)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
               
                   var v = wfDB.FM_Form.Where(p => p.formName == formName && p.businessID == businessID).ToList();
                   return v;
          
            }
         }

        //12.9	√得到表单版本列表
        public List<FM_Form> getFormEditionList(string formID)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {

                var v = wfDB.FM_Form.Where(p => p.formID == formID).ToList();
                return v;

            }
        }

        //12.10	√得到表单
        public FormV getFormV(string formID, string editEdition)
        {
            FormV formv = new FormV();

            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (wfDB.FM_Form_V.Count(p => p.formID == formID && p.editEdition == editEdition) != 1)
                {
                    return null;
                    
                }
                else
                {
                    formv.formHard = wfDB.FM_Form_V.Single(p => p.formID == formID && p.editEdition == editEdition);
                    formv.items = wfDB.FM_Item_V.Where(p => p.formID == formv.formHard.formID && p.editEdition==formv.formHard.editEdition).ToList();
                    return formv;
                }
            }



            
        }

        //12.11	√上传文件
        public string  upFile(string filename, byte[] content)
        {
            try
            {
                filename = System.Guid.NewGuid().ToString();
                string filePath = MyConfig.getAppValue("filepath") +filename;
                System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
                fs.Write(content, 0, content.Length);
                fs.Flush();
                fs.Close();
                fs.Dispose();
                fs = null;

                return filename;
            }
            catch
            {
                return "";
            }
        }

        //12.12	√下载文件
        public byte[] downFile(string fileID)
        {
            string filePath = MyConfig.getAppValue("filepath") +fileID;
            try
            {
                System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);

                int n = (int)fs.Length;

                byte[] content = new byte[n];
                fs.Read(content, 0, n);

                fs.Close();


                return content;
            }
            catch
            {
                return null;
            }
        }

        public string getDataFormFiled(Guid guid,string fieldName,string expression )
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
              var v=  wfDB.FM_Item_V.FirstOrDefault(p => p.instanceID == guid && p.itemGuid.ToString() == fieldName);
              if (v != null)
              {
                  return v.itemValue;
              }
            }
            return "err";
        }

    }

    [DataContract()]
    public class FormTemplet
    {
        [DataMember()]
        public FM_Templet templet
        {
            set;
            get;
        }

        [DataMember()]
        public List<FM_TempletItem> templetItem
        { set; get; }
    }

    [DataContract()]
    public class Form
    {
        [DataMember()]
        public FM_Form formHard
        {
            set;
            get;

        }

        [DataMember()]
        public List<FM_Item> items
        {
            set;
            get;
        }
    }



    [DataContract()]
    public class FormV
    {
        [DataMember()]
        public FM_Form_V formHard
        {
            set;
            get;

        }

        [DataMember()]
        public List<FM_Item_V> items
        {
            set;
            get;
        }
    }
}
